import { Component, OnInit, ViewChild } from '@angular/core';
import { VipService } from '../../service/vip.service';

@Component({
    selector: 'app-vip-behavior',
    templateUrl: './behavior.component.html',
    styleUrls: ['./behavior.component.scss']
})
export class VipBehaviorComponent implements OnInit {
    @ViewChild('grid') grid;

    constructor(private vipService: VipService) { };

    behaviors: any[];
    pageNumber: number = 1;
    pageSize: number = 8;
    totalPage: number = 0;
    searchParam: string = '';
    isReady: boolean = false;

    ngOnInit() {
        this.initGrid(1);
    };

    prevPage() {
        this.pageNumber = this.pageNumber - 1;
        this.clear();
        this.initGrid(0);
    };

    nextPage() {
        this.pageNumber = this.pageNumber + 1;
        this.clear();
        this.initGrid(0);
    };

    // 表格构建
    initGrid(status) {
        let grid = this.grid.nativeElement;
        this.vipService.behavior(
            this.pageNumber.toString(),
            this.pageSize.toString(),
            this.searchParam
        ).then(result => {
            if (result.code === '10000') {
                this.behaviors = result.data.list;
                if (status) {
                    this.totalPage = Math.ceil(+result.data.totalPage / this.pageSize);
                }
                grid.columns[2].renderer = cell => {
                    switch (cell.data) {
                        case 0: cell.element.textContent = '增加'; break;
                        case 1: cell.element.textContent = '消费'; break;
                        case 2: cell.element.textContent = '充值'; break;
                        case 3: cell.element.textContent = '赠送'; break;
                        case 4: cell.element.textContent = '其他'; break;
                    };
                };
                this.isReady = true;
            }
        });
    };
    
    clear(){
        this.behaviors = [];
        this.isReady = false;
    }
}
